package modelo.persistencia;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

@SuppressWarnings("deprecation")
public class HibernateUtil {
    
    private static final SessionFactory sessionFactory;
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    static {
        try {
             sessionFactory = new AnnotationConfiguration().
                     configure("/modelo/persistencia/hibernate.cfg.xml").buildSessionFactory();                     
            } catch (Throwable ex) {
               // Log the exception. 
               System.err.println("Inciação da criação do SessionFactory falhou." + ex);
               throw new ExceptionInInitializerError(ex);
            }
      }

    public static Session getInstance()throws HibernateException { 
        Session session = (Session)threadLocal.get();
        session = sessionFactory.openSession();
        threadLocal.set(session);
        return session;        
    }
}//fim hibernate util
